﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace FTFSmartCar
{
    public partial class IR2d : Form
    {


        byte[,] IRData;

        int HistoryLen  = 50;

        int DataPtr;

        public IR2d()
        {
            InitializeComponent();
            IRData = new byte[16, HistoryLen];
        }

        public void PostNextIRData(byte [] IRDataIn)
        {
            for (int i = 0; i < 16; i++)
            {
                IRData[i, DataPtr] = IRDataIn[i];
                
            }

            DataPtr++;
            if(DataPtr >= HistoryLen)
            {
                DataPtr = 0;
            }

            byte[,] Temp = new byte[16, HistoryLen];

            int CurrentIdx = DataPtr;

            for (int i = 0; i < HistoryLen; i++)
            {
                for (int j = 0; j < 16; j++)
                {
                    Temp[j, i] = IRData[j, CurrentIdx]; 
                }

                CurrentIdx++;
                if (CurrentIdx >= HistoryLen)
                {
                    CurrentIdx = 0;
                }

            }
            IRIntensity.SetData(Temp);
            this.Invalidate();
        }

        private void IR2d_FormClosing(object sender, FormClosingEventArgs e)
        {
            e.Cancel = true;
            this.Hide();
        }

        private void IR2d_Resize(object sender, EventArgs e)
        {
            ResizeGraph();
        }

        void ResizeGraph()
        {
            IRIntensity.Location = new Point(10, 10);
            // Leave a small margin around the outside of the control
            IRIntensity.Size = new Size(ClientRectangle.Width - 20,
                                    ClientRectangle.Height - 20);

            this.Invalidate();
        }
    }
}
